package com.itheima.web.controller.cargo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.cargo.ExportProduct;
import com.itheima.domain.cargo.ExportProductExample;
import com.itheima.domain.cargo.PackingList;
import com.itheima.domain.cargo.PackingListExample;
import com.itheima.service.cargo.ExportProductService;
import com.itheima.service.cargo.ExportService;
import com.itheima.service.cargo.PackingListService;
import com.itheima.web.controller.BaseController;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.*;

/**
 *  @Author : ItAmao
 *  @Date   : 2020/9/7 21:47
 */
@Controller
@RequestMapping("/cargo/packing")
public class PackingListController extends BaseController {

    @Reference
    private PackingListService packingListService;

    @Reference
    private ExportService exportService;

    @Reference
    private ExportProductService exportProductService;

    @RequestMapping(value = "/list", name = "装箱单列表查询")
    public String list(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize, String message1) {
        PackingListExample packingListExample = new PackingListExample();
        packingListExample.createCriteria().andCompanyIdEqualTo(getCompanyId());
        PageInfo<PackingList> page = packingListService.findByPage(pageNum, pageSize, packingListExample);
        request.setAttribute("page", page);

        return "cargo/packing/export-packingList";
    }

    @RequestMapping(value = "/toPacking", name = "跳转装箱单新增页面")
    public String toPacking(String id) {
        request.setAttribute("id", id);
        return "cargo/packing/export-toPacking";
    }

    @RequestMapping(value = "/toUpdate", name = "跳转装箱单修改页面")
    public String toUpdate(String id) {
        //1.通过id查询的装箱单信息
        PackingList packingList = packingListService.selectByPrimaryKey(id);
        request.setAttribute("packingList", packingList);
        //查询装箱单下的货物信息
        String[] exportIds = packingList.getExportIds().split(",");
        ArrayList<ExportProduct> packingProducts = new ArrayList<>();

        for (String exportId : exportIds) {
            //根据exportIds 查询报运单下的货物
            List<ExportProduct> list = exportProductService.findByExportId(exportId);
            for (ExportProduct exportProduct : list) {
                packingProducts.add(exportProduct);

            }
        }
        request.setAttribute("eps", packingProducts);
        return "cargo/packing/export-packingUpdate";
    }

    @RequestMapping(value = "/edit", name = "装箱单的新增/修改")
    public String edit(PackingList packingList, String[] exportIds) {
        if (StrUtil.isEmpty(packingList.getPackingListId())) {
            //表示新增
            //1.设置装箱id
            packingList.setPackingListId(UUID.randomUUID().toString());
            //2.设置公司id
            packingList.setCompanyId(getCompanyId());
            packingList.setCompanyName(getCompanyName());
            //3.设置装箱单的状态,新增为0
            packingList.setState(0L);
            //4.设置发票日期
            packingList.setInvoiceDate(new Date());
            //5.进行保存
            packingListService.insert(packingList, exportIds);
        } else {
            packingListService.updateByPrimaryKeySelective(packingList);
        }
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping(value = "/toView", name = "查看装箱信息")
    public String toView(String id) {
        // 查询当前装箱单信息
        PackingList packingList = packingListService.selectByPrimaryKey(id);
        //查询装箱单下的货物信息
        String[] exportIds = packingList.getExportIds().split(",");
        ArrayList<ExportProduct> packingProducts = new ArrayList<>();

        for (String exportId : exportIds) {
            //根据exportIds 查询报运单下的货物
            List<ExportProduct> list = exportProductService.findByExportId(exportId);
            for (ExportProduct exportProduct : list) {
                packingProducts.add(exportProduct);

            }
        }
        request.setAttribute("packingList", packingList);
        request.setAttribute("eps", packingProducts);
        return "/cargo/packing/export-packingView";
    }

    @RequestMapping(value = "/delete", name = "删除装箱")
    public String delete(String id) {
        //1.根据id删除
        packingListService.deleteByPrimaryKey(id);
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping(value = "/packingPdf", name = "查看报表")
    public void packingPdf(String id) throws JRException, IOException {
        //1. 读取模板
        String realPath = session.getServletContext().getRealPath("/jasper/packing.jasper");

        PackingList packingList = packingListService.selectByPrimaryKey(id);
        Map<String, Object> map = BeanUtil.beanToMap(packingList);
        //查询装箱单下的货物----list
        String exportIds = packingList.getExportIds();
        List<String> exportIdList = Arrays.asList(exportIds.split(","));
        ExportProductExample exportProductExample = new ExportProductExample();
        exportProductExample.createCriteria().andExportIdIn(exportIdList);
        List<ExportProduct> list = exportProductService.findAll(exportProductExample);

        //3 填充jasper模板(jasper文件和data组装)
        JasperPrint jasperPrint = JasperFillManager.fillReport(realPath, map, new JRBeanCollectionDataSource(list));
        //输出到浏览器
        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
    }
}
